package com.framemodule.utils;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.framemodule.R;
import com.nostra13.universalimageloader.core.ImageLoader;


/**
 * Created by pc on 2016/4/29.
 */
@SuppressLint("ValidFragment")
public class DoubleButtonDialog extends DialogFragment {

    public DoubleButtonDialog() {}
    private EditText et;
    private final int DIAPHANEITY = 200;
    public DoubleButtonDialog(OnDilaogClickListener clickListener) {
        this.mClickListener = clickListener;
    }

    public DoubleButtonDialog(OnDialogEditListener mEditListener) {
        this.mEditListener = mEditListener;
    }

    public interface OnDilaogClickListener{
        void onDismiss(android.support.v4.app.DialogFragment fragment);
        void onClick(android.support.v4.app.DialogFragment fragment, View v);
    }
    public interface OnDialogEditListener{
        void onDismiss(android.support.v4.app.DialogFragment fragment);
        void onClick(String edit);
    }
    private OnDialogEditListener mEditListener;
    private OnDilaogClickListener mClickListener;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.translucent_dialog);
        Bundle bundle = getArguments();
        dialog.setCancelable(false);//使双按钮对话框点击屏幕不可被取消
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
                }
                return true;
            }
        });
        dialog.setContentView(R.layout.dialog_doublebtn_layout);
        TextView titleTxtV = (TextView)dialog.findViewById(R.id.dialog_title);
        TextView contentTxtV = (TextView)dialog.findViewById(R.id.dialog_note);
        final EditText edit = (EditText)dialog.findViewById(R.id.dialog_edit);
        et = edit;
        Button button1 = (Button)dialog.findViewById(R.id.dialog_cancel);
        Button button2 = (Button)dialog.findViewById(R.id.dialog_ok);
        dialog.findViewById(R.id.double_top).getBackground().setAlpha(DIAPHANEITY);
        dialog.findViewById(R.id.dialog_ok).getBackground().setAlpha(DIAPHANEITY);
        dialog.findViewById(R.id.dialog_cancel).getBackground().setAlpha(DIAPHANEITY);

        if (bundle.getBoolean("edit")){
            dialog.findViewById(R.id.dialog_note).setVisibility(View.GONE);
            dialog.findViewById(R.id.dialog_edit).setVisibility(View.VISIBLE);
            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismissAllowingStateLoss();
                    if(mEditListener != null)mEditListener.onDismiss(DoubleButtonDialog.this);
                }
            });
            button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DoubleButtonDialog.this.dismissAllowingStateLoss();
                    if(mEditListener != null)mEditListener.onClick(edit.getText().toString().trim());
                }
            });
        }else {
            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismissAllowingStateLoss();
                    if(mClickListener != null)mClickListener.onDismiss(DoubleButtonDialog.this);
                }
            });
            button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DoubleButtonDialog.this.dismissAllowingStateLoss();
                    if(mClickListener != null)mClickListener.onClick(DoubleButtonDialog.this,v);
                }
            });
        }
        if (bundle.getInt("id",-1)!=-1){
            dialog.findViewById(R.id.dialog_image).setVisibility(View.VISIBLE);
            dialog.findViewById(R.id.dialog_title).setVisibility(View.GONE);
            ((ImageView)dialog.findViewById(R.id.dialog_image)).setBackgroundResource(bundle.getInt("id"));
        }
        if (bundle.getString("path")!=null){
            dialog.findViewById(R.id.dialog_image1).setVisibility(View.VISIBLE);
            dialog.findViewById(R.id.dialog_title).setVisibility(View.GONE);
            ImageLoader.getInstance().displayImage(bundle.getString("path"),(ImageView) dialog.findViewById(R.id.dialog_image1));
            if (bundle.getString("path2")!=null){
                dialog.findViewById(R.id.dialog_image2).setVisibility(View.VISIBLE);
                ImageLoader.getInstance().displayImage(bundle.getString("path2"),(ImageView) dialog.findViewById(R.id.dialog_image2));
            }

        }
        if (bundle.getString("content2")!=null){
            dialog.findViewById(R.id.dialog_note).setVisibility(View.GONE);
            dialog.findViewById(R.id.dialog_note1).setVisibility(View.VISIBLE);
            dialog.findViewById(R.id.dialog_note2).setVisibility(View.VISIBLE);
            ((TextView)dialog.findViewById(R.id.dialog_note1)).setText(bundle.getString("content"));
            ((TextView)dialog.findViewById(R.id.dialog_note2)).setText(bundle.getString("content2"));
        }
        if (bundle.getString("title")!=null&&bundle.getString("title").equals("设置密码")){
            dialog.findViewById(R.id.v_doublebtn1).setBackgroundColor(Color.BLACK);
            dialog.findViewById(R.id.v_doublebtn).setBackgroundColor(Color.BLACK);
            ((TextView)dialog.findViewById(R.id.dialog_cancel)).setTextColor(Color.BLACK);
            ((TextView)dialog.findViewById(R.id.dialog_ok)).setTextColor(Color.BLACK);
            edit.setHint("输入4位数字密码");
            edit.setInputType(InputType.TYPE_CLASS_NUMBER);

        }


        titleTxtV.setText(bundle.getString("title"));
        contentTxtV.setText(bundle.getString("content"));
        button1.setText(bundle.getString("leftBtn"));
        button2.setText(bundle.getString("rightBtn"));
        return dialog;
    }
    public static DoubleButtonDialog show(FragmentManager manager, String title, String content, String leftBtn, String rightBtn, OnDilaogClickListener click){
        Fragment fragment = manager.findFragmentByTag("DOUBLE_DIALOG");
        FragmentTransaction ft = manager.beginTransaction();
        if(fragment != null){
            ft.remove(fragment);
        }
        DoubleButtonDialog dialog = new DoubleButtonDialog(click);
        Bundle bundle = new Bundle();
        bundle.putString("title",title);
        bundle.putString("content",content);
        bundle.putString("leftBtn",leftBtn);
        bundle.putString("rightBtn",rightBtn);
        dialog.setArguments(bundle);
        ft.add(dialog,"DOUBLE_DIALOG");
        ft.commitAllowingStateLoss();
        return dialog;
    }
    public static DoubleButtonDialog show(int id, FragmentManager manager, String title, String content, String leftBtn, String rightBtn, OnDilaogClickListener click){
        Fragment fragment = manager.findFragmentByTag("DOUBLE_DIALOG");
        FragmentTransaction ft = manager.beginTransaction();

        if(fragment != null){
            ft.remove(fragment);
        }

        DoubleButtonDialog dialog = new DoubleButtonDialog(click);
        Bundle bundle = new Bundle();
        bundle.putInt("id",id);
        bundle.putString("title",title);
        bundle.putString("content",content);
        bundle.putString("leftBtn",leftBtn);
        bundle.putString("rightBtn",rightBtn);
        dialog.setArguments(bundle);
        ft.add(dialog,"DOUBLE_DIALOG");
        ft.commitAllowingStateLoss();
        return dialog;
    }
    public static DoubleButtonDialog show(String path, String path2, FragmentManager manager, String title, String content, String content2, String leftBtn, String rightBtn, OnDilaogClickListener click){
        Fragment fragment = manager.findFragmentByTag("DOUBLE_DIALOG");
        FragmentTransaction ft = manager.beginTransaction();

        if(fragment != null){
            ft.remove(fragment);
        }

        DoubleButtonDialog dialog = new DoubleButtonDialog(click);
        Bundle bundle = new Bundle();
        bundle.putString("path",path);
        bundle.putString("path2",path2);
        bundle.putString("title",title);
        bundle.putString("content",content);
        bundle.putString("content2",content2);
        bundle.putString("leftBtn",leftBtn);
        bundle.putString("rightBtn",rightBtn);
        dialog.setArguments(bundle);
        ft.add(dialog,"DOUBLE_DIALOG");
        ft.commitAllowingStateLoss();
        return dialog;
    }

    /**
     * 两个按钮中部是EditText
     *
     * @param manager
     * @param title
     * @param leftBtn
     * @param rightBtn
     * @param click
     * @param edit
     * @return
     */
    public static DoubleButtonDialog show(FragmentManager manager, String title, String leftBtn, String rightBtn, boolean edit , OnDialogEditListener click){
        Fragment fragment = manager.findFragmentByTag("DOUBLE_DIALOG");
        FragmentTransaction ft = manager.beginTransaction();
        if(fragment != null){
            ft.remove(fragment);
        }
        DoubleButtonDialog dialog = new DoubleButtonDialog(click);
        Bundle bundle = new Bundle();
        bundle.putString("title",title);
        bundle.putBoolean("edit",edit);
        bundle.putString("leftBtn",leftBtn);
        bundle.putString("rightBtn",rightBtn);
        dialog.setArguments(bundle);
        ft.add(dialog,"DOUBLE_DIALOG");
        ft.commitAllowingStateLoss();
        return dialog;
    }

}
